﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;


/// <summary>
/// 返回結果
/// </summary>
[DataContract]
class JSON_Result {
    /// <summary>
    /// 公車ID
    /// </summary>
    [DataMember(Order = 0)]
    public int id { get; set; }
    /// <summary>
    /// 公車名稱
    /// </summary>
    [DataMember(Order = 1)]
    public string name { get; set; }
    /// <summary>
    /// 所屬地區
    /// </summary>
    [DataMember(Order = 2)]
    public string city { get; set; }
    /// <summary>
    /// 
    /// </summary>
    [DataMember(Order = 3)]
    public string from { get; set; }
    /// <summary>
    /// 
    /// </summary>
    [DataMember(Order = 4)]
    public string to { get; set; }
}
public partial class Service_QRoute : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["q"] != null && Request.QueryString["q"] != string.Empty)
        {
            using (MyDb.Db db = new MyDb.Db())
            {
                IList<JSON_Result> list = new List<JSON_Result>();
                JSON_Result item = null;

                using (DataTable dt = db.getDataTable(string.Format("select distinct(pathAttributename),pathattributeid,departureZh,destinationZh,city from route where nameZh like '%{0}%' order by pathattributename asc", Request.QueryString["q"]), "mess"))
                {
                    foreach (DataRow rs in dt.Rows)
                    {
                        item = new JSON_Result();
                        item.city = rs["city"].ToString();
                        item.name = rs["pathAttributename"].ToString();
                        item.id = Convert.ToInt32(rs["pathattributeid"]);
                        //item.com_name=rs["providerName"].ToString();
                        item.from=rs["departureZh"].ToString();
                        item.to=rs["destinationZh"].ToString();
                        list.Add(item);
                    }
                }
                
                Response.Write(string.Format("{{\"route\":{0}}}", MyDb.JSON.stringify(list)));
                list.Clear();
                list = null;
            }
        }
        Response.End();
    }
}
